//
//  LoginViewController.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/4/14.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistereVC.h"

@interface LoginViewController ()<UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    self.errorLabel.hidden = YES;
    self.accountTextFiled.delegate = self;
    self.passwordTextFiled.delegate = self;
    [self.bgImage setImage:[UIImage imageNamed:@"bg"]];

    //设置返回按钮
    [self.backButton setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [self.backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
    //设置登录按钮
    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateHighlighted];
    [self.loginButton setTitle:@"登   录" forState:UIControlStateNormal];
    [self.loginButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.loginButton setTitleColor:ColorWithRGB(255, 255, 255, 1) forState:UIControlStateNormal];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置状态栏颜色黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark - buttonClink

- (IBAction)backButtonClink:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //未登录时，返回上一级tabbar
    AppDelegate *app = kAppDelegate;
    NSLog(@"index = %ld",app.customTabBar.index);
    app.customTabBar.selectedIndex = app.customTabBar.index;
    app.window.rootViewController  = app.customTabBar;
}

- (IBAction)loginButtonClink:(id)sender
{
    NSString *userName = self.accountTextFiled.text;
    NSString *password = self.passwordTextFiled.text;
    if ([userName isEqualToString:@""] || [password isEqualToString:@""]) {
        self.errorLabel.text = @"用户名或者密码不能为空！";
    } else {
        
        NSString *url = [NSString stringWithFormat:@"%@%@",API_BASE_URL,API_USER_LOGIN_URL];
        NSDictionary *parameters = @{@"username": userName,@"password":password,@"grant_type":@"password"};
        
        [HttpClient loginOrRegistWithUrl:url parameters:parameters success:^(id json) {
            
            NSLog(@"%@",json);
            NSLog(@"message = :%@",json[@"token_type"]);
            //返回推出页
            [self dismissViewControllerAnimated:YES completion:nil];

        } fail:^{
            
            NSLog(@"请求失败！");
        }];
    }
    
}

- (IBAction)registerButtonClink:(id)sender
{
    RegistereVC *registerVC = [[RegistereVC alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)forgetButtonClink:(id)sender
{
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
